package com.zsrt.common.listener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * 在线人数的模块
 * @author 罗智壕-13424431416
 *
 * 2017年6月14日
 */
public class SessionListener implements HttpSessionListener{
	
	/**
	 * session创建（人数++）
	 */
	@Override
	public void sessionCreated(HttpSessionEvent se) {
		HttpSession session = se.getSession();
		ServletContext application = session.getServletContext();
		Object counter = application.getAttribute("counter");
		if(null == counter){
			application.setAttribute("counter", 1);
		}else{
			application.setAttribute("counter", (Integer)counter+1);
		}
		
	}
	
	/**
	 * session销毁（人数--）
	 */
	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		HttpSession session = se.getSession();
		ServletContext application = session.getServletContext();
		Object counter = application.getAttribute("counter");
		if(null == counter){
			application.setAttribute("counter", 0);
		}else{
			if((Integer)counter > 0){
				application.setAttribute("counter", (Integer)counter-1);
			}
		}
	}

}
